/*
 * CS_ENTER_MATCH_ROOM.cpp
 *
 *  Created on: 2013. 9. 25.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Battle.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Party.h"

namespace ProjectZ { namespace NCommand {

void enterRoomPracticeMatch(User * pUser)
{
	::Network::Packet * pAck	= ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << static_cast<uint8_t>(rand() % 4);
	*pAck << static_cast<uint16_t>(pUser->GetBattle()->GetData().degree);	// 방향 벡터(보고있는 방향 0 : RIGHT, 90 : DOWN..)
	*pAck << static_cast<uint16_t>(pUser->GetBattle()->GetData().x);
	*pAck << static_cast<uint16_t>(pUser->GetBattle()->GetData().y);
	*pAck << static_cast<uint16_t>(PVP_MAP_ID);	// 세팅될 맵

	pAck->MakePacket(CMD_SN_ENTER_ROOM);
	pUser->Send(pAck);
}

void Command::CS_ENTER_MATCH_ROOM(User * pUser, ::Network::Packet * pPacket)
{
	/*CS_ENTER_MATCH_ROOM				0x1310				// VS 화면 연출 끝난후에 요청함 (ZNO_SN_ENTER_ROOM를 내려 받아야함)
	{
		u8		u8is_booster;							//본인 부스터 사용 여부 ( 0: 사용안함, 1: 사용함 )
	}

	//대전 완료시 입장 할 Room Info 전달
	SN_PVP_ENTER_ROOM						0x0606
	{
		s8	s8random_break_obj_value;			// 랜덤오브젝트 값 : -1 없음 : 0 ~ 3 사이 값
		u16	u16degree;							// 방향 벡터(보고있는 방향 0 : RIGHT, 90 : DOWN..)
		u16	u16pos_x;							// 세팅될맵좌표
		u16	u16pos_y;							// 세팅될맵좌표
		u16	u16map_idx;							// 세팅될 맵
	}*/


	PREPARE_PACKET;

	char useBoost	= 0;

	*pPacket >> useBoost;

	NLogic::Space * pSpace = pUser->GetSpace();
	if (pSpace == NULL)
	{
		DEBUGLOG("CS_ENTER_MATCH_ROOM : pSpace is NULL!!");
		return;
	}

	// AI 대전인 경우
	if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::PVP_PRACTICE)
	{
		enterRoomPracticeMatch(pUser);
		return;
	}

	NLogic::Party * pParty = pSpace->GetParty();
	if (NULL == pParty)
	{
		DEBUGLOG("CS_ENTER_MATCH_ROOM : pParty is NULL!!");
		return;
	}

	pUser->GetBattle()->GetData().bUseBoost = useBoost;
	pUser->GetBattle()->GetData().bRecvEnterRoom = true;

	bool bReady = true;

	// 모든 유저가 준비 상태인지 확인 한다.
	const NLogic::Space::USERTREE & clsUserTree = pSpace->GetUserTree();
	NLogic::Space::USERTREE::const_iterator citer = clsUserTree.begin();

	for( ; citer!=clsUserTree.end() ; citer++ )
	{
		User * pUser = (*citer).second;

		if (false == pUser->GetBattle()->GetData().bRecvEnterRoom)
		{
			bReady = false;
			break;
		}
	}

	// 모든 유저가 준비 상태가 아닌 경우
	if (false == bReady)
		return;

	for (citer = clsUserTree.begin(); citer != clsUserTree.end(); citer++)
	{
		User * pOther = (*citer).second;

		// 현재 AI 대전인 경우에는 AI를 수행중인 유저에게는 패킷을 보내지 않는다
		if (pOther->GetEnumState() == User::EnumState::PVP_READY
				|| pOther->GetEnumState() == User::EnumState::PVP_BATTLE
				|| pOther->GetEnumState() == User::EnumState::PVP_BATTLE_END)
		{
			continue;
		}

		::Network::Packet * pAck	= ::Network::GETPACKETPOOL()->SEND_NEW();

		*pAck << static_cast<uint8_t>(rand() % 4);
		*pAck << static_cast<uint16_t>(pOther->GetBattle()->GetData().degree);	// 방향 벡터(보고있는 방향 0 : RIGHT, 90 : DOWN..)
		*pAck << static_cast<uint16_t>(pOther->GetBattle()->GetData().x);
		*pAck << static_cast<uint16_t>(pOther->GetBattle()->GetData().y);
		*pAck << static_cast<uint16_t>(PVP_MAP_ID);	// 세팅될 맵

		pAck->MakePacket(CMD_SN_ENTER_ROOM);
		pOther->Send(pAck);
	}

	// Party 상태 변경
	if (pParty->GetState() == NLogic::Party::State::PVP_ENTERROOM_WAIT)
	{
		pParty->SetState(NLogic::Party::State::PVP_READY_WAIT);
	}

	pUser->SetEnumState(User::EnumState::PVP_READY);
}

} /*NCommand*/ } /*ProjectZ*/
